#include "Boss.h"
#include "Bullet.h"
#include "CSGD_TextureManager.h"
#include "CSGD_XAudio2.h"
#include "MainMenuState.h"
#include "LevelManager.h"
#include "EntityManager.h"
#include "MessageSystem.h"
#include "Game.h"
#include "SGD_Math.h"


CBoss::CBoss(void)
{
	m_eType = BOSS;
	SetHealth(1000);
	SetAttackCool(0.5f);
	SetSpecAttCool(30);
	SetBaseDmg(40);
	SetSpecialDmg(100);

	SetWidth(614);
	SetHeight(500);

	SetImage( CSGD_TextureManager::GetInstance()->LoadTexture(_T("resource/Images/SingleRobot.png")) );

	SetTeam( 2 );

	// CHANGE - hard-coded beginning position
	SetPosX( 800 - (float)GetWidth()/2 );
	SetPosY( (600 - (float)GetHeight())/2 );
	SetVelX( -10 );
	SetVelY( 0 );
}


CBoss::~CBoss(void)
{
}


/////////////////////////////////////////////////////////////////////////////
// Function		: Update
// Parameters	: dT - Time elapsed since last update
// Notes		: Updates the unit's variables based on elapsed time
/////////////////////////////////////////////////////////////////////////////
void CBoss::Update(float dT){
	//update cooldowns	
	SetAttackCool(GetAttackCool() - dT);
	SetSpecAttCool(GetSpecAttCool() - dT);
	//Check for a target

	//Check if special attack has cooled down
	if(GetSpecAttCool() <= 0.0f){
		//Perform special attack
		//Send message through system
		SetAttackCool(1.0f);
	}
	//otherwise check if it can attack normally
	if(GetAttackCool() <= 0.0f)
	{
		Shoot();
		SetAttackCool(0.5f);
	}

	CMovingObject::Update(dT);

	// End game check
	// CHANGE - hard-coded
	// Lose
	if( GetPosX() < 50 )
	{
		CMessage* msg = new CMessage();
		msg->SetMessageID(MSG_END_GAME);
		CGame::GetInstance()->GetMessageSystem()->GetInstance()->SendNewMessage(msg);

		CSGD_XAudio2::GetInstance()->MusicStopSong(CGame::GetInstance()->GetMusicID());
		CSGD_XAudio2::GetInstance()->MusicPlaySong(CLevelManager::GetInstance()->GetCurrLevel()->GetTeam1()->GetBase()->GetLose());
		CMainMenuState::GetInstance()->SetWinOrLose(true);
	}
	// Win
	else if( GetPosX() > 800 )
	{
		CMessage* msg = new CMessage();
		msg->SetMessageID(MSG_END_GAME);
		CGame::GetInstance()->GetMessageSystem()->GetInstance()->SendNewMessage(msg);

		CSGD_XAudio2::GetInstance()->MusicStopSong(CGame::GetInstance()->GetMusicID());
		CSGD_XAudio2::GetInstance()->MusicPlaySong(CLevelManager::GetInstance()->GetCurrLevel()->GetTeam1()->GetBase()->GetWin());
		CMainMenuState::GetInstance()->SetWinOrLose(false);
	}
}
/////////////////////////////////////////////////////////////////////////////
// Function		: Render
// Notes		: Renders the unit to the screen
/////////////////////////////////////////////////////////////////////////////
void CBoss::Render(){
	CMovingObject::Render();
}

/////////////////////////////////////////////////////////////////////////////
// Function		: PushBack
// Parameters	: d - the damage dealt to the boss, determines how far back it moves
// Notes		: Pushes the boss backwards
/////////////////////////////////////////////////////////////////////////////
void CBoss::PushBack( int d )
{
	SetPosX( GetPosX() + d/2 );
}

/////////////////////////////////////////////////////////////////////////////
// Function		: Shoot
// Notes		: shoots bullets
/////////////////////////////////////////////////////////////////////////////
void CBoss::Shoot()
{

	//Perform normal attack
	//Send message through system
	CBullet* bullet = new CBullet( this );
	CSGD_XAudio2::GetInstance()->SFXPlaySound(GetShotID());
	bullet->SetImage( CSGD_TextureManager::GetInstance()->LoadTexture( _T("resource/Images/Bullet.png") ) );
	bullet->SetWidth(8);
	bullet->SetHeight(8);
	bullet->SetPath(0);
	bullet->SetDamage( rand() % ((int)GetBaseDmg()) );
	bullet->SetDeathY( 600 );

	// CHANGE - hard-coded bullet stuff
	bullet->SetPosX( GetPosX() + 160 );
	bullet->SetPosY( GetPosY() + 190 );
	int y = rand() % 350 + 225;

	tVector2D vTarget = { (float)(rand() % (int)GetPosX()), (float)y };

	tVector2D vToTarget = { vTarget.fX - bullet->GetPosX(), 
		vTarget.fY - bullet->GetPosY()};
	vToTarget = Vector2DNormalize(vToTarget);

	bullet->SetVelX( vToTarget.fX * 150 );
	bullet->SetVelY( vToTarget.fY * 150 );

	CEntityManager::GetInstance()->AddEntity(bullet);
}